package rr_server;

import java.net.*;
import java.util.*;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
	
public class Server {
	
//	ServerSocket serverSocket;
//	String connectedUsers[] = {"", "", ""};
	int firstPitcherId = 0;
	int secondPitcherId = 0;
	int thirdPitcherId = 0;
	int curPitcherId;
	int player1Result;
	int player2Result;
	int player3Result;
	int[] results = {-1, -1, -1};
	
	public void manageGame() {
		
		manageFirstRound(firstPitcherId);
		manageSecondRound(secondPitcherId);
		manageThirdRound(thirdPitcherId);
		
		int orderCode;
		orderCode = MessageUtil.getOrderCode();
		if (orderCode == 1) {
			firstPitcherId = 1;
			secondPitcherId = 2;
			thirdPitcherId = 3;
		} else if (orderCode == 2) {
			firstPitcherId = 1;
			secondPitcherId = 3;
			thirdPitcherId = 2;
		} else if (orderCode == 3) {
			firstPitcherId = 2;
			secondPitcherId = 1;
			thirdPitcherId = 3;
		} else if (orderCode == 4) {
			firstPitcherId = 2;
			secondPitcherId = 3;
			thirdPitcherId = 1;
		} else if (orderCode == 5) {
			firstPitcherId = 3;
			secondPitcherId = 1;
			thirdPitcherId = 2;
		} else {
			firstPitcherId = 3;
			secondPitcherId = 2;
			thirdPitcherId = 1;
		}

	}

	public void manageFirstRound(int firstPitcherId) {
		ScheduledFuture<?> timeOutScenario1;
		ScheduledFuture<?> timeOutScenario2;
		ScheduledThreadPoolExecutor stpe1;
		ScheduledThreadPoolExecutor stpe2;
		Date timeOutDate1 = new Date();
		Date timeOutDate2 = new Date();
		 
		long timeOutTime1 = timeOutDate1.getTime() - System.currentTimeMillis();
		long timeOutTime2 = timeOutDate2.getTime() - System.currentTimeMillis();
		timeOutDate1.setTime(timeOutTime1);
		timeOutDate2.setTime(timeOutTime2);
		sendToAll(MessageUtil.makeUpdateMsg(1, firstPitcherId, -1, -1, -1));
			
		// (!stillTiming15) || (phraseReceived == true)
		stpe1 = new ScheduledThreadPoolExecutor(1);
		timeOutScenario1 = stpe1.schedule(new TimeOutManager(), 15, TimeUnit.SECONDS);
		
		// (!stillTiming55) || (results[1] != -1)
		stpe2 = new ScheduledThreadPoolExecutor(1);
		timeOutScenario2 = stpe2.schedule(new TimeOutManager(), 55, TimeUnit.SECONDS);	
	
		
	}

	public void manageSecondRound(int secondPitcherId) {
		player1Result = results[0];
		player2Result = results[1];
		player3Result = results[2];	
		sendToAll(MessageUtil.makeUpdateMsg(2, secondPitcherId, player1Result, player2Result, player3Result));
		for (int i = 0; i < 3; i++) { // Reset array to hold default values
			results[i] = -1;
		}

		
		
	}
	
	public void manageThirdRound(int thirdPitcherId) {
		player1Result = results[0];
		player2Result = results[1];
		player3Result = results[2];
		sendToAll(MessageUtil.makeUpdateMsg(3, thirdPitcherId, player1Result, player2Result, player3Result));
		for (int i = 0; i < 3; i++) { // Reset array to hold default values
			results[i] = -1;
		}
		
		
		
		player1Result = results[0];
		player2Result = results[1];
		player3Result = results[2];	
		sendToAll(MessageUtil.makeUpdateMsg(-1, -1, player1Result, player2Result, player3Result));
		for (int i = 0; i < 3; i++) { // Reset array to hold default values
		results[i] = -1;
		}
	}
	
	private class TimeOutManager implements Runnable {
		
		public void run() {
			sendToAll(MessageUtil.makeErrorMsg(1));
		}

	}
	
	private class TaskA implements Runnable {		
		public void run() {		
		}
	}

	private class TaskB implements Runnable {	
		public void run() {		
		}
	}

	public void sendToPlayer1(String message) {
		
	}
	
	public void sendToPlayer2(String message) {
		
	}
	
	public void sendToPlayer3(String message) {
		
	}
	
	public void sendToAll(String message) {
		sendToPlayer1(message);
		sendToPlayer2(message);
		sendToPlayer3(message);
	}
	
}
